var app = getApp();
Page({
    data:{
        isIPX : app.globalData.isIPX,
        citylarr : [],
        agentcode : '',
        citytype : '区县',
        citylevel : 0,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onShow(){
        if( this.data.needback ){
            wx.navigateBack();
        }
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            options : options,
            agentcode : options.agentcode ? options.agentcode : (options.scene ? options.scene : ''),
        });
        app.com.auth(function(user){
            app.com.http('ctyagent','POST',{op:'joininfo'},0,false,'','',function(res){
                if (!res.data.errno) {

                    var typelist = ['区县','市级'];
                    var citylevel = 0;
                    if(res.data.data.params.ctypp*1 > 0){
                        typelist.push('省级');
                    }
                    if(res.data.data.params.wxptp*1 > 0){
                        var citylevel = 1;
                        typelist.splice(0,1);
                    }
                    self.setData({
                        params : res.data.data.params,
                        inited : true,
                        typelist : typelist,
                        citylevel : citylevel,
                    });
                    
                    wx.setNavigationBarTitle({
                      title:  '申请'+res.data.data.params.ctybname,
                    });
                    
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '',
            path: '',
            imageUrl : '',
        }
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    bindRegionChange : function(e){
        var city = e.detail.value;
        var len = this.data.citylarr.length;
        if(this.data.citylevel == 0 && len >= 3 ){
            app.com.alert('最多选择3个区县');
            return false;
        }
        if(this.data.citylevel == 1 && len >= 3 ){
            app.com.alert('最多选择3个城市');
            return false;
        }
        if(this.data.citylevel == 2 && len >= 3 ){
            app.com.alert('最多选择3个省份');
            return false;
        }
        for (var i = 0; i <this.data.citylarr.length; i++) {

            if( this.data.citylevel == 0 ){
                if( this.data.citylarr[i][0] != city[0] || this.data.citylarr[i][1] != city[1] ){
                    app.com.alert('选择的城市必须在同一个市辖区下');
                    return false;
                }
                if( this.data.citylarr[i][2] == city[2] ){
                    app.com.alert('此城市已经选择了');
                    return false;
                }
            }else if(this.data.citylevel == 1){
                if( this.data.citylarr[i][0] != city[0] ){
                    app.com.alert('选择的城市必须在同一个省区下');
                    return false;
                }
                if( this.data.citylarr[i][1] == city[1] ){
                    app.com.alert('此城市已经选择了');
                    return false;
                }
            }else if(this.data.citylevel == 2){
                if( this.data.citylarr[i][0] == city[0] ){
                    app.com.alert('此省份已经选择了');
                    return false;
                }
            }
        }
        this.data.citylarr.push(city);
        this.setData({
            citylarr : this.data.citylarr
        })
    },
    delcity: function (e) {
        this.data.citylarr.splice(e.currentTarget.dataset.i,1);
        this.setData({
            citylarr : this.data.citylarr
        })
    },
    changetype(e){
        var type = this.data.typelist[e.detail.value];
        this.setData({
            citytype : type,
            citylevel : type == '区县' ? 0 : ( type == '市级' ? 1 : 2 ),
            citylarr : [],
        })
    },
    submit(e){
        var self = this;
        var postdata = {
            op : 'subjoin',
            citylarr : JSON.stringify(self.data.citylarr),
            name : e.detail.value.name,
            tel : e.detail.value.tel,
            level : this.data.citylevel,
            agentcode : e.detail.value.agentcode,
            pass : e.detail.value.pass,
        };
        self.subMess();

        if( self.data.citylarr.length <= 0 ){
            app.com.alert('你还没选择城市');
            return false;
        }
        app.com.http('ctyagent','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data ) {

                if( res.data.data.ispay == 1 ){
                    wx.requestPayment({
						'appId' : res.data.data.appId,
                        'timeStamp': res.data.data.timeStamp,
                        'nonceStr': res.data.data.nonceStr,
                        'package': res.data.data.package,
                        'signType': 'MD5',
                        'paySign': res.data.data.paySign,
                        'success': function (payres) {
                            if( payres.errMsg == 'requestPayment:ok' ){
                                app.com.alert(res.data.data.mess,function(){
                                    wx.navigateBack();
                                });
                                return false;
                            }
                        },
                        'fail': function (payres) {
                            
                        }
                    });
                }else{
                    app.com.alert(res.data.message,function(){
                        wx.navigateBack();
                    });
                }
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    selectarea(){
        this.selectComponent('#ausarea').show();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    subMess(){
        var self = this;
        if(wx.requestSubscribeMessage){
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.submess,
                complete : function(){
                    
                }
            })
        }       
    }

})